package lbs;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import object.Order;

import service.CommonServlet;

public class GetOrders extends CommonServlet {
	
	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			String sql = "select r_order.order_id, r_order.order_d, r_order.end_d, restaurant_info.id, restaurant_info.city, restaurant_info.longitude, restaurant_info.latitude, restaurant_info.recommend_dishes, r_products.name from r_order,restaurant_info,r_products ";
			sql+="where r_order.u_no = ? and r_order.r_id = restaurant_info.id and r_order.r_id = r_products.id ";

			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setInt(1, uNo);
			
			ResultSet rs = pstmt.executeQuery();
			List<Order> orders = new ArrayList<Order>();
			while(rs.next())
			{
				Order order = new Order();
				order.setCity(rs.getString("restaurant_info.city"));
				order.setLongitude(rs.getFloat("restaurant_info.longitude"));
				order.setLatitude(rs.getFloat("restaurant_info.latitude"));
				order.setName(rs.getString("r_products.name"));
				order.setRecommend(rs.getString("restaurant_info.recommend_dishes"));
				order.setId(rs.getInt("r_order.order_id"));
				order.setOrderDate(rs.getString("r_order.order_d"));
				order.setEndDate(rs.getString("r_order.end_d"));
				order.setrId(rs.getString("restaurant_info.id"));
				
				orders.add(order);
			}

			sendObject(orders, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Constructor of the object.
	 */
	public GetOrders() {
		super();
	}

}
